package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;


@RestController
@RequestMapping("/checkitem")
@Slf4j
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem){
        log.info("[检查项-新增]data:{}",checkItem);
        try {
            checkItemService.add(checkItem);
            return new Result(true, MessageConst.ADD_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            log.error("[检查项-新增]",e);
            return new Result(false,MessageConst.ADD_CHECKITEM_FAIL);
        }
    }
    @GetMapping("/findPage")
    //@PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public PageResult findPage(QueryPageBean queryPageBean){
        log.info("[检查项-分页查询]data:{}",queryPageBean);
        try {
            return checkItemService.findPage(queryPageBean);
        } catch (RuntimeException e) {
            log.error("[检查项-分页查询]异常",e);
            return new PageResult(0L, Collections.emptyList());
        }
    }
    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result delete(Integer id){
        log.info("[检查项-删除]id:{}",id);
        try {
            checkItemService.delete(id);
            return new Result(true, MessageConst.DELETE_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            log.error("[检查项-根据id删除]",e);
            return new Result(false,MessageConst.DELETE_CHECKITEM_FAIL,":"+e.getMessage());
        }

    }
    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findById(Integer id){
        try {
            CheckItem checkItem=checkItemService.findById(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,checkItem);
        } catch (RuntimeException e) {
            log.error("",e);
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }
    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    public Result edit(@RequestBody CheckItem checkItem){
        log.info(" edit checkItem:{}",checkItem);
        try {
            checkItemService.edit(checkItem);
            return new Result(true, MessageConst.EDIT_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            log.error("[检查项-新增]",e);
            return new Result(false,MessageConst.EDIT_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("/findAll")
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    public Result findAll(){
        log.info("[检查项-查询所有]");
        try {
            List<CheckItem> checkItemList=checkItemService.findAll();
            return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,checkItemList);
        } catch (RuntimeException e) {
            log.error("",e);
            return new Result(false,MessageConst.QUERY_CHECKITEM_FAIL+":"+e.getMessage());
        }
    }

}
